import { isNumber } from "lodash";
import { ReportParagraphBlock, VegetationAndEcosystemAnalysisParams } from "./typings/index";
import { generateWordParagraBlockByPlainText, generateWordParagraBlockByTableGenerateResult } from "./utils";


/**
 * 植被与生态系统类型统计分析
 * @param sheetData 
 * @returns 
 */
const vegetationAndEcosystemAnalysis = (params: VegetationAndEcosystemAnalysisParams): ReportParagraphBlock[] => {
    let reportParagraphBlockList: ReportParagraphBlock[] = [];
    try {
        const {
            ecosystemTypeSheetData,
            vegetationTypeSheetData,
        } = params;
        console.log("vegetationAndEcosystem---->params", params)
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText('植被和生态系统类型统计分析报告', 'heading1'));
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText('一、生态系统类型', 'heading6'));
        //【森林生态系统】、【灌丛生态系统】，面积分别为【122】、【35】公顷。
        let ecosystemTypeParagraphText = `依据《中国植被》提出的植物群落分类系统，参考《中国生态系统》的分类方法，本区生态系统类型共${ecosystemTypeSheetData.length}种，主要为`;
        ecosystemTypeSheetData.forEach((row, rowIndex) => {
            ecosystemTypeParagraphText += row['生态系统类型'];
            if (rowIndex < ecosystemTypeSheetData.length - 1) {
                ecosystemTypeParagraphText += '、';
            } else {
                ecosystemTypeParagraphText += '，面积分别为';
            }
        })
        let ecosystemTypeTableData = [
            ["生态系统类型", "面积（公顷）"],
        ]
        ecosystemTypeSheetData.forEach((row, rowIndex) => {
            ecosystemTypeParagraphText += row['面积（公顷）'];
            ecosystemTypeTableData.push([row['生态系统类型'], row['面积（公顷）']]);
            if (rowIndex < ecosystemTypeSheetData.length - 1) {
                ecosystemTypeParagraphText += '、';
            } else {
                ecosystemTypeParagraphText += '公顷。';
            }
        })
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText(ecosystemTypeParagraphText, 'normalParagraph'));
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText('生态系统类型统计表', 'smallTableName'));
        reportParagraphBlockList.push(generateWordParagraBlockByTableGenerateResult({
            tableStyle: 'normalTable',
            tableData: ecosystemTypeTableData,
        }))
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText('二、植被类型', 'heading6'));
        let uniquenVegetationTypeGroupValueList: { name: string, area: number }[] = [];
        let uniquenVegetationTypeValueList: { name: string, area: number }[] = [];
        let uniquenFormationValueList: { name: string, area: number }[] = [];
        let _vegetationTypeSheetData = vegetationTypeSheetData.filter(row => {
            return row['面积（公顷）'] && isNumber(row['面积（公顷）']) && Number(row['面积（公顷）']) > 0
        })
        _vegetationTypeSheetData.forEach((row, rowIndex) => {
            const tempVegetationTypeGroup = row['植被型组'];
            const tempVegetationType = row['植被型'];
            const tempFormation = row['植被群系和亚群系'];
            let findVegetationTypeGroupIndex = -1;
            let findVegetationTypeIndex = -1;
            let findFormationIndex = -1;
            uniquenVegetationTypeGroupValueList.forEach((ele, index) => {
                if (ele.name == tempVegetationTypeGroup) {
                    findVegetationTypeGroupIndex = index;
                }
            })
            uniquenVegetationTypeValueList.forEach((ele, index) => {
                if (ele.name == tempVegetationType) {
                    findVegetationTypeIndex = index;
                }
            })
            uniquenFormationValueList.forEach((ele, index) => {
                if (ele.name == tempFormation) {
                    findFormationIndex = index;
                }
            })
            if (findVegetationTypeGroupIndex != -1) {
                uniquenVegetationTypeGroupValueList[findVegetationTypeGroupIndex].area += Number(row['面积（公顷）']);
            } else {
                uniquenVegetationTypeGroupValueList.push({
                    area: Number(row['面积（公顷）']),
                    name: tempVegetationTypeGroup,
                })
            }
            //
            if (findVegetationTypeIndex != -1) {
                uniquenVegetationTypeValueList[findVegetationTypeIndex].area += Number(row['面积（公顷）']);
            } else {
                uniquenVegetationTypeValueList.push({
                    area: Number(row['面积（公顷）']),
                    name: tempVegetationType,
                })
            }
            //
            if (findFormationIndex != -1) {
                uniquenFormationValueList[findFormationIndex].area += Number(row['面积（公顷）']);
            } else {
                uniquenFormationValueList.push({
                    area: Number(row['面积（公顷）']),
                    name: tempFormation,
                })
            }
        })
        let vegetationTypeParagraphText = `根据本地区植被的特点，在考虑生境特点的前提下，以植物群落本身的特征，特别是群落的优势种或建群种为分类依据，将植被划分为${uniquenVegetationTypeGroupValueList.length}个植被型组，${uniquenVegetationTypeValueList.length}个植被型，${uniquenFormationValueList.length}个群系。本地区主要的植被型为`;
        uniquenVegetationTypeGroupValueList = uniquenVegetationTypeGroupValueList.sort((a, b) => {
            return a.area - b.area
        })
        uniquenFormationValueList = uniquenFormationValueList.sort((a, b) => {
            return a.area - b.area
        })
        uniquenVegetationTypeGroupValueList.forEach((ele, index) => {
            if (index < 2) {
                vegetationTypeParagraphText += ele.name;
                if (index < 1) {
                    vegetationTypeParagraphText += '和';
                } else {
                    vegetationTypeParagraphText += '等，主要的群系为';
                }
            }
        })
        uniquenFormationValueList.forEach((ele, index) => {
            if (index < 3) {
                vegetationTypeParagraphText += ele.name;
                if (index < 2) {
                    vegetationTypeParagraphText += '和';
                } else {
                    vegetationTypeParagraphText += '等，详见下表。';
                }
            }
        })
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText(vegetationTypeParagraphText, 'normalParagraph'));
        reportParagraphBlockList.push(generateWordParagraBlockByPlainText('植被类型统计表', 'smallTableName'));
        let statisticalTableData = [
            ["植被型组", "植被型", "植被群系和亚群系", "面积（公顷）"]
        ]
        _vegetationTypeSheetData.forEach(row => {
            statisticalTableData.push([
                row['植被型组'],
                row['植被型'],
                row['植被群系和亚群系'],
                row['面积（公顷）'] + '',
            ])
        })
        reportParagraphBlockList.push(generateWordParagraBlockByTableGenerateResult({
            tableStyle: 'normalTable',
            tableData: statisticalTableData
        }))
        console.log("reportParagraphBlockList--->", reportParagraphBlockList)
        return reportParagraphBlockList;
    } catch (e) {
        console.log("vegetationAndEcosystem--->err", e)
        return reportParagraphBlockList;
    }
}


export default vegetationAndEcosystemAnalysis;